﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Behavour : MonoBehaviour
{
    private float spd;

    private Rigidbody2D rigidBody;

    private float jumpForce,walkForce,runForce;
    
    // Start is called before the first frame update
    void Start()
    {
        jumpForce = 5;
        walkForce = 50;
        runForce = 40;
        this.rigidBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    private void Update()
    { 
        Move();//wasd
    }

    private void Move()
    {
        if (Input.GetKeyDown(KeyCode.W)|| Input.GetKeyDown(KeyCode.UpArrow))
        {
            rigidBody.AddForce(Vector2.up * jumpForce,ForceMode2D.Impulse); //给物体一个向上10的瞬时力
        }
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            rigidBody.AddForce(Vector2.left * walkForce,ForceMode2D.Force); //给物体一个向左10的瞬时力
        }
        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            rigidBody.AddForce(Vector2.right * walkForce,ForceMode2D.Force); //给物体一个向右10的瞬时力
        }
    }

    private bool Detect()
    {
        Vector3 origin = transform.position;
        Vector3 fwd = transform.TransformDirection(Vector2.up);
        Vector3 left = transform.TransformDirection(Vector2.left);
        Vector3 right = transform.TransformDirection(Vector2.right);
        return Physics.Raycast(origin, fwd, 1)&&Physics.Raycast(origin, left, 1)
                                              &&Physics.Raycast(origin, right, 1);
    }
    
}